import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { CoreModule } from './../core.module';
import { NzNotificationService, NzMessageService } from 'ng-zorro-antd';
import { timeout } from 'rxjs/operators';
import { urlService } from './url';
import { Router } from '@angular/router';

@Injectable({
    providedIn: CoreModule,
})
export class ApiService {
    constructor(
        private message: NzMessageService, // lee 添加
        private http: HttpClient,
        private notification: NzNotificationService,
        private router: Router
    ) {}

    get(url: string, params?: { [key: string]: any }, noFail?: Boolean): Promise<Function> {
        const request = this.http
            .get(urlService('dev', url), {
                params: params,
            })
            .pipe(timeout(240000))
            .toPromise();
        return new Promise((resolve, reject) => {
            request
                .then((res: any) => {
                    if (res.status === '0') {
                        resolve(res);
                    } else {
                        reject(res);
                        if (!noFail) {
                            this.message.create('error', res.message);
                        }
                    }
                })
                .catch(res => {
                    if (res.status === 403) {
                        this.notification.create('error', '登录超时，请重新登录', '系统提示',{nzKey:'timeout'});
                        this.router.navigateByUrl('/member-center/home/login');
                        localStorage.removeItem('user');
                    } else {
                        this.notification.create('error', '未知错误，请联系管理员', '系统提示');
                    }
                });
        });
    }
    post(url: string, data?: { [key: string]: any }, params?: { [key: string]: any }, noFail?: Boolean): Promise<Function> {
        const request = this.http
            .post(urlService('dev', url), data, {
                params: params,
            })
            .pipe(timeout(60000))
            .toPromise();
        return new Promise((resolve, reject) => {
            request
                .then((res: any) => {
                    if (res.status === '0') {
                        resolve(res);
                    } else {
                        reject(res);
                        if (!noFail) {
                            this.message.create('error', res.message);
                        }
                    }
                })
               
                .catch(res => {
                    // console.log(res)
                    if (res.status === 403) {
                        this.notification.create('error', '登录超时，请重新登录', '系统提示',{nzKey:'timeout'});
                        this.router.navigateByUrl('/member-center/home/login');
                        localStorage.removeItem('user');
                    } else {
                        this.notification.create('error', '未知错误，请联系管理员', '系统提示');
                    }
                });
        });
    }
}
